//
//  QiubaiCell.m
//  Together
//
//  Created by Wang Shuguang on 12-12-19.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "QiubaiCell.h"
#import "UIImageView+WebCache.h"

@implementation QiubaiCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        textContentView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -10, SCREEN_WIDTH, 10)];
        textContentView.image = [[UIImage imageNamed:@"qiubai_block_background.png"] stretchableImageWithLeftCapWidth:160 topCapHeight:15];
        [self addSubview:textContentView];
        [textContentView release];
        
        // 来源
        nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/2 - 10, 20)];
        nameLbl.font = [UIFont systemFontOfSize:14.0f];
        nameLbl.textColor = [UIColor colorWithRed:57.0/255.0f green:70.0f/255.0f blue:83.0/255.0f alpha:1.0];
        [self addSubview:nameLbl];
        [nameLbl setBackgroundColor:[UIColor clearColor]];
        [nameLbl release];
        
        // 发布日期
        timeLbl = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2 - 10, 20)];
        timeLbl.font = [UIFont systemFontOfSize:12.0f];
        timeLbl.textColor = [UIColor colorWithRed:244.0/255.0f green:137.0f/255.0f blue:13.0/255.0f alpha:1.0];
        timeLbl.textAlignment = UITextAlignmentRight;
        [timeLbl setBackgroundColor:[UIColor clearColor]];
        [self addSubview:timeLbl];
        [timeLbl release];
        
        
        // 内容
        textLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, SCREEN_WIDTH - 20, 20)];
        textLbl.font = [UIFont systemFontOfSize:14.0f];
        textLbl.textColor = [UIColor colorWithRed:66.0/255.0f green:66.0/255.0f blue:66.0/255.0f alpha:1.0];
        textLbl.lineBreakMode = UILineBreakModeWordWrap;
        textLbl.numberOfLines = 0;
        [textLbl setBackgroundColor:[UIColor clearColor]];
        [self addSubview:textLbl];
        [textLbl release];
        
        // 图片
        imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.frame = CGRectMake(10, textLbl.frame.origin.y + textLbl.frame.size.height, SCREEN_WIDTH - 20, 0);
        [self addSubview:imageView];
        [imageView release];
        
        
       
    }
    return self;
}

- (void) setName:(NSString *)name{
    [nameLbl setText:name];
}

- (void) setTime:(NSString *)time{
    [timeLbl setText:time];
}

- (void) setContent:(NSString *)text{
    CGSize size = CGSizeMake(textLbl.frame.size.width, 10000);
    size = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
    textLbl.frame = CGRectMake(textLbl.frame.origin.x, textLbl.frame.origin.y, textLbl.frame.size.width, size.height + 10);
    
    [textLbl setText:text];
    
    imageView.frame = CGRectMake(imageView.frame.origin.x, textLbl.frame.origin.y + textLbl.frame.size.height, imageView.frame.size.width, imageView.frame.size.height);
    
    if (imageView.hidden) {
        textContentView.frame = CGRectMake(0, -10, SCREEN_WIDTH, imageView.frame.origin.y + 30);
    }else{
        textContentView.frame = CGRectMake(0, -10, SCREEN_WIDTH, imageView.frame.origin.y + imageView.frame.size.height + 30);
    }
}

- (void) setImageWithURL:(NSURL *)url imageSize:(CGSize)imageSize{
    if (imageSize.height <= 0) {
        imageView.hidden = YES;
    }else{
        imageView.hidden = NO;
        CGSize realImageSize = CGSizeMake(SCREEN_WIDTH - 20, (SCREEN_WIDTH - 20) * imageSize.height / imageSize.width);
        imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, realImageSize.width, realImageSize.height);
        
        [imageView setImageWithURL:url placeholderImage:nil options:SDWebImageLowPriority];
    }
    
    if (imageView.hidden) {
        textContentView.frame = CGRectMake(0, -10, SCREEN_WIDTH, imageView.frame.origin.y + 30);
    }else{
        textContentView.frame = CGRectMake(0, -10, SCREEN_WIDTH, imageView.frame.origin.y + imageView.frame.size.height + 30);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end